package cn.edu.ujn.bmis.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cn.edu.ujn.bmis.model.SysFunction;
import cn.edu.ujn.bmis.model.UserFunction;
import cn.edu.ujn.bmis.model.UserInfo;
import cn.edu.ujn.bmis.service.SysFunctionManager;
import cn.edu.ujn.bmis.service.UserFunctionManager;
import cn.edu.ujn.bmis.service.UserInfoManager;
import cn.edu.ujn.bmis.tools.Helper;

import com.opensymphony.xwork2.ActionSupport;

@Component("UserFunction")
@Scope("prototype")
public class UserFunctionAction extends ActionSupport {

	private static final long serialVersionUID = 8611649067399433460L;
	private UserFunctionManager userFunctionManager;
	private Helper helper;
	private List<UserFunction> userFunctionList=new ArrayList<UserFunction>();
	private String targetType = "navTab"; // navTab或dialog，用来标记是navTab上的分页还是dialog上的分页
	private int pageNum = 1;
	private int currentPage = 1; // 当前是第几页
	private int totalCount = 0; // 总条数
	private int numPerPage = 5; // 每页显示多少条
	private int pageNumShown = 5; // 页标数字多少个
	private int pageCount = 0; // 总页数
	private String orderField;
	private String orderDirection;
	private UserFunction userFunction;
	private String userId;
	private String functionNum;
	private long id;
	private String status;
	private List<SysFunction> sysFunctionList = new ArrayList<SysFunction>();
	private SysFunctionManager sysFunctionManager;
	private UserInfoManager userInfoManager;
	private String functionName;
	
	
	/**
	 * 全部授权准备工作
	 */
	public String beforeAddAllUserFunction(){
		//功能权限验证
		if(!helper.checkFunction(functionNum)){
			Helper.out("403", "该用户没有此功能权限!", "", "", "", "");
		}
		return SUCCESS;
	}
	
	/**
	 * 全部解权准备工作
	 */
	public String beforeDelAllUserFunction(){
		//功能权限验证
		if(!helper.checkFunction(functionNum)){
			Helper.out("403", "该用户没有此功能权限!", "", "", "", "");
		}
		return SUCCESS;
	}
	
	public void delAllUserFunction(){
		//检查输入的读者编号是否存在
		String hql_check="";
		List<UserInfo> userInfoList=new ArrayList<UserInfo>();
		if(userId!=null&&!"".equals(userId)){
			hql_check="from UserInfo u where u.userId='"+userId.trim()+"'";
			userInfoList=userInfoManager.findUserInfo(hql_check);
			if(userInfoList.size()<=0){
				Helper.out("300", "输入的用户编号不存在请检查！", "", "", "", "");
				return;
			}
		}
		String hql="";
		hql="delete from UserFunction u where u.userInfo.userId='"+userId.trim()+"'";
		if(userFunctionManager.update(hql)){
			Helper.out("200", "用户解权成功", "closeCurrent", "", "", "findUserFunction");
		}else{
			Helper.out("300", "用户授权失败", "", "", "", "");
		}
	}
	
	/**
	 * 对用户全部授权
	 */
	public void addAllUserFunction(){
		String hql="";
		String status="";
		boolean b=false;
		int count=0;
		//检查输入的读者编号是否存在
		String hql_check="";
		List<UserInfo> userInfoList=new ArrayList<UserInfo>();
		if(userId!=null&&!"".equals(userId)){
			hql_check="from UserInfo u where u.userId='"+userId.trim()+"'";
			userInfoList=userInfoManager.findUserInfo(hql_check);
			if(userInfoList.size()<=0){
				Helper.out("300", "输入的用户编号不存在请检查！", "", "", "", "");
				return;
			}else{
				status=userInfoList.get(0).getUserStatus();
			}
		}
		hql = "select s from SysFunction s where not exists("
			+ "select 1 from UserFunction u where s.functionNum=u.sysFunction.functionNum "
			+ "and u.userInfo.userId='" + userId.trim() + "')";
		if("读者".equals(status)){
			hql+=" and s.functionNum like 'hxlreader%'";
		}else if("图书管理员".equals(status)){
			hql+=" and s.functionNum like 'hxlbookad%'";
		}else if("系统管理员".equals(status)){
			hql+=" and s.functionNum like 'hxlsysad%'";
		}
		sysFunctionList=sysFunctionManager.findSysFunction(hql);
		for(int i=0;i<sysFunctionList.size();i++){
			UserFunction u=new UserFunction();
			UserInfo userInfo=new UserInfo();
			u.setSysFunction(sysFunctionList.get(i));
			userInfo.setUserId(userId);
			u.setUserInfo(userInfo);
			b=userFunctionManager.saveUserFunction(u);
			if(b){
				count++;
			}
		}
		if(count==sysFunctionList.size()){
			Helper.out("200", "用户授权成功", "closeCurrent", "", "", "findUserFunction");
		}else{
			Helper.out("300", "用户授权失败", "", "", "", "");
		}
		
	}
	
    /**
     * 对用户进行授权
     */
    public void addUserFunction(){
    	//功能权限验证
		if(!helper.checkFunction(functionNum)){
			Helper.out("403", "该用户没有此功能权限!", "", "", "", "");
			return;
		}
    	//检查输入的读者编号是否存在
		String hql_check="";
		List<UserInfo> userInfoList=new ArrayList<UserInfo>();
		if(userId!=null&&!"".equals(userId)){
			hql_check="from UserInfo u where u.userId='"+userId.trim()+"'";
			userInfoList=userInfoManager.findUserInfo(hql_check);
			if(userInfoList.size()<=0){
				Helper.out("300", "输入的用户编号不存在请检查！", "", "", "", "");
				return;
			}
		}
    	if(userFunctionManager.saveUserFunction(userFunction)){
    		Helper.out("200", "用户授权成功", "", "", "", "findSysFunction");
    	}else{
    		Helper.out("300", "用户授权失败", "", "", "", "");
    	}
    }
    
    /**
     * 查看用户的功能信息
     */
    @SuppressWarnings("unchecked")
	public String findUserFunction(){
    	String hql="";
		String hql_size="";
    	//功能权限验证
		if(!helper.checkFunction(functionNum)){
			Helper.out("403", "该用户没有此功能权限!", "", "", "", "");
		}
		this.currentPage = this.pageNum;
		hql="select uf from UserFunction uf,SysFunction s,UserInfo u where " +
				"uf.userInfo.userId=u.userId and uf.sysFunction.functionNum=s.functionNum";
		hql_size="select count(*) from UserFunction uf,SysFunction s,UserInfo u where " +
		"uf.userInfo.userId=u.userId and uf.sysFunction.functionNum=s.functionNum";
		if(userId!=null&&!"".equals(userId)){
			hql+=" and u.userId='"+userId.trim()+"'";
			hql_size+=" and u.userId='"+userId.trim()+"'";
		}
		if(functionName!=null&&!"".equals(functionName.trim())){
			hql+=" and s.functionName like '%"+functionName.trim()+"%' ";
			hql_size+=" and s.functionName like '%"+functionName.trim()+"%' ";
		}
		totalCount = helper.findSize(hql_size);
		pageCount = totalCount % numPerPage == 0 ? totalCount / numPerPage
				: totalCount / numPerPage + 1;
		userFunctionList = helper.fenYe(hql, this.currentPage, this.numPerPage);
    	return SUCCESS;
    }
    
    /**
     * 用户功能权限解权
     */
    public void delUserFunction(){
    	//功能权限验证
		if(!helper.checkFunction(functionNum)){
			Helper.out("403", "该用户没有此功能权限!", "", "", "", "");
			return;
		}
    	String hql="";
    	hql="delete from UserFunction u where u.id="+id+"";
    	if(userFunctionManager.update(hql)){
    		Helper.out("200", "用户解权成功", "", "", "", "findUserFunction");
    	}else{
    		Helper.out("300", "用户解权失败", "", "", "", "");
    	}
    }
	
	public Helper getHelper() {
		return helper;
	}
	
	@Resource(name="helper")
	public void setHelper(Helper helper) {
		this.helper = helper;
	}

	public UserFunctionManager getUserFunctionManager() {
		return userFunctionManager;
	}

	@Resource(name = "userFunctionManager")
	public void setUserFunctionManager(UserFunctionManager userFunctionManager) {
		this.userFunctionManager = userFunctionManager;
	}
	
	public List<UserFunction> getUserFunctionList() {
		return userFunctionList;
	}

	public String getTargetType() {
		return targetType;
	}

	public void setTargetType(String targetType) {
		this.targetType = targetType;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getNumPerPage() {
		return numPerPage;
	}

	public void setNumPerPage(int numPerPage) {
		this.numPerPage = numPerPage;
	}

	public int getPageNumShown() {
		return pageNumShown;
	}

	public void setPageNumShown(int pageNumShown) {
		this.pageNumShown = pageNumShown;
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public String getOrderField() {
		return orderField;
	}

	public void setOrderField(String orderField) {
		this.orderField = orderField;
	}

	public String getOrderDirection() {
		return orderDirection;
	}

	public void setOrderDirection(String orderDirection) {
		this.orderDirection = orderDirection;
	}

	public UserFunction getUserFunction() {
		return userFunction;
	}

	public void setUserFunction(UserFunction userFunction) {
		this.userFunction = userFunction;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getFunctionNum() {
		return functionNum;
	}

	public void setFunctionNum(String functionNum) {
		this.functionNum = functionNum;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public List<SysFunction> getSysFunctionList() {
		return sysFunctionList;
	}
	
	@Resource(name="sysFunctionManager")
	public void setSysFunctionManager(SysFunctionManager sysFunctionManager) {
		this.sysFunctionManager = sysFunctionManager;
	}

	public UserInfoManager getUserInfoManager() {
		return userInfoManager;
	}
	
	@Resource(name="userInfoManager")
	public void setUserInfoManager(UserInfoManager userInfoManager) {
		this.userInfoManager = userInfoManager;
	}

	public String getFunctionName() {
		return functionName;
	}

	public void setFunctionName(String functionName) {
		this.functionName = functionName;
	}
	
}
